package com.theobserver.theobserver;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.AVFile;
import com.avos.avoscloud.AVObject;
import com.avos.avoscloud.AVQuery;
import com.avos.avoscloud.AVUser;
import com.avos.avoscloud.FindCallback;
import com.avos.avoscloud.GetDataCallback;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by wssxk on 7/8/17.
 */

public class News {

    public final static int NEWS_TYPE_NONE = 0;
    public final static int NEWS_TYPE_REFER = 1;
    public final static int NEWS_TYPE_CUSTOM = 2;


    public final static int NEWS_INFO_COVER = 21;
    public final static int NEWS_INFO_REPORTER = 22;
    public final static int NEWS_INFO_CATALOG = 23;

    public final static int NEWS_TRUE = 1;
    public final static int NEWS_FALSE = 0;

    public final static int NEWS_ERROR_NONE = 10;
    public final static int NEWS_ERROR_NORESULT = 11;
    public final static int NEWS_ERROR_CATCHEXCEPTIONG = 12;

    /*
    public int new_id = 0;
    public String news_title = null;
    public int news_type = NEWS_TYPE_NONE;
    public String news_content = null;
    public int news_like_count = 0;
    public int news_comment_count = 0;
    public int news_has_image = NEWS_FALSE;
    public String news_url = null;
    public String news_date = null;
    public String news_reporter = null;
    public int news_catalog = 0;
    */
    public AVObject cur_news = null;

    public Bitmap news_cover = null;
    public String reporter_name = null;
    public String news_catalog = null;

    public NewsCallBack callBack = null;
    public AVException error = null;
    public int completeResult = 0;

    NewsGroup news_group = null;


    //获取指定数量的全部新闻
    static public void getAllNews(int from, int len, NewsCallBack cb)
    {
        final AVQuery<AVObject> query = new AVQuery<>("news");
        query.limit(len);       //长度
        query.skip(from-1);     //跳过的条数
        query.include("news_catalog");
        query.include("news_reporter");
        final NewsCallBack tcb = cb;
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                if(tcb != null)
                {
                    if(e != null)
                    {
                        tcb.getNews(null, News.NEWS_ERROR_CATCHEXCEPTIONG, e);
                        return;
                    }
                    else if(list.size() == 0){
                        //ArrayList<News> news_list = new ArrayList<>();
                        tcb.getNews(null, News.NEWS_ERROR_NORESULT, null);
                        return;
                    }
                    else {
                        News tmp_news = null;
                        NewsGroup ng = new NewsGroup(tcb);
                        for (int t = 0; t < list.size(); t ++)
                        {
                            tmp_news = new News();
                            ng.news_group.add(tmp_news);
                            tmp_news.cur_news = list.get(t);
                            tmp_news.news_group = ng;
                            //tmp_news.completeData(tcb);
                            //news_list.add(tmp_news);
                        }
                        for (int t = 0; t < list.size(); t ++)
                        {
                            ng.news_group.get(t).completeData(tcb);
                        }
                        //这里必须先添加到news_group再启动completeData
                    }
                        //tcb.getNews(news_list, 1, e);


                }
            }
        });


    }

    //获取指定分类的新闻
    static public void getCatalogNews(AVObject ca, int from, int len, NewsCallBack cb)
    {

        final AVQuery<AVObject> query = new AVQuery<>("news");
        query.limit(len);
        query.skip(from-1);
        query.include("news_catalog");
        query.include("news_reporter");
        query.whereEqualTo("news_catalog", ca);
        final NewsCallBack tcb = cb;
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                if(tcb != null)
                {
                    if(e != null)
                    {
                        tcb.getNews(null, News.NEWS_ERROR_CATCHEXCEPTIONG, e);
                        return;
                    }
                    else if(list.size() == 0){
                        //ArrayList<News> news_list = new ArrayList<>();
                        tcb.getNews(null, News.NEWS_ERROR_NORESULT, null);
                        return;
                    }
                    else {
                        News tmp_news = null;
                        NewsGroup ng = new NewsGroup(tcb);
                        for (int t = 0; t < list.size(); t ++)
                        {
                            tmp_news = new News();
                            ng.news_group.add(tmp_news);
                            tmp_news.cur_news = list.get(t);
                            tmp_news.news_group = ng;
                            //tmp_news.completeData(tcb);
                            //news_list.add(tmp_news);
                        }
                        for (int t = 0; t < list.size(); t ++)
                        {
                            ng.news_group.get(t).completeData(tcb);
                        }
                    }
                    //tcb.getNews(news_list, 1, e);


                }
            }
        });

    }

    static public void getColumnNews(AVObject column, NewsCallBack cb)
    {

        final AVQuery<AVObject> query = new AVQuery<>("column_news");
        //query.limit(len);
        //query.skip(from-1);
        query.include("news_id");
        query.include("news_id.news_reporter");
        query.include("news_id.news_catalog");
        //query.whereEqualTo("news_catalog", ca);
        query.whereEqualTo("column_id", column);
        final NewsCallBack tcb = cb;
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                if(tcb != null)
                {

                    if(list != null && list.size() != 0)
                    {
                        NewsGroup ng = new NewsGroup(tcb);
                        News t_news = null;
                        for(int t = 0; t < list.size(); t ++)
                        {
                            t_news = new News();
                            t_news.cur_news = list.get(t).getAVObject("news_id");
                            ng.news_group.add(t_news);
                            t_news.news_group = ng;
                        }

                        for(int t = 0; t < list.size(); t ++)
                        {
                            ng.news_group.get(t).completeData(tcb);
                        }
                    }
                    else
                    {
                        tcb.getNews(null, 0, null);
                    }


                    /*
                    if(e != null)
                    {
                        tcb.getNews(null, News.NEWS_ERROR_CATCHEXCEPTIONG, e);
                        return;
                    }
                    else if(list.size() == 0){
                        //ArrayList<News> news_list = new ArrayList<>();
                        tcb.getNews(null, News.NEWS_ERROR_NORESULT, null);
                        return;
                    }
                    else {
                        News tmp_news = null;
                        NewsGroup ng = new NewsGroup(tcb);
                        for (int t = 0; t < list.size(); t ++)
                        {
                            tmp_news = new News();
                            ng.news_group.add(tmp_news);
                            tmp_news.cur_news = list.get(t);
                            tmp_news.news_group = ng;
                            //tmp_news.completeData(tcb);
                            //news_list.add(tmp_news);
                        }
                        for (int t = 0; t < list.size(); t ++)
                        {
                            ng.news_group.get(t).completeData(tcb);
                        }
                    }*/
                    //tcb.getNews(news_list, 1, e);


                }
            }
        });


    }

    static  public void getCollectionNews(AVUser user, NewsCallBack cb)
    {

        final AVQuery<AVObject> query = new AVQuery<>("collect");
        //query.limit(len);
        //query.skip(from-1);
        query.include("news_id");
        query.include("news_id.news_reporter");
        query.include("news_id.news_catalog");
        //query.whereEqualTo("news_catalog", ca);
        query.whereEqualTo("user_email", user);
        final NewsCallBack tcb = cb;
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                if(tcb != null)
                {

                    if(list != null && list.size() != 0)
                    {
                        NewsGroup ng = new NewsGroup(tcb);
                        News t_news = null;
                        for(int t = 0; t < list.size(); t ++)
                        {
                            t_news = new News();
                            t_news.cur_news = list.get(t).getAVObject("news_id");
                            ng.news_group.add(t_news);
                            t_news.news_group = ng;
                        }

                        for(int t = 0; t < list.size(); t ++)
                        {
                            ng.news_group.get(t).completeData(tcb);
                        }
                    }
                    else
                    {
                        tcb.getNews(null, 0, null);
                    }


                    /*
                    if(e != null)
                    {
                        tcb.getNews(null, News.NEWS_ERROR_CATCHEXCEPTIONG, e);
                        return;
                    }
                    else if(list.size() == 0){
                        //ArrayList<News> news_list = new ArrayList<>();
                        tcb.getNews(null, News.NEWS_ERROR_NORESULT, null);
                        return;
                    }
                    else {
                        News tmp_news = null;
                        NewsGroup ng = new NewsGroup(tcb);
                        for (int t = 0; t < list.size(); t ++)
                        {
                            tmp_news = new News();
                            ng.news_group.add(tmp_news);
                            tmp_news.cur_news = list.get(t);
                            tmp_news.news_group = ng;
                            //tmp_news.completeData(tcb);
                            //news_list.add(tmp_news);
                        }
                        for (int t = 0; t < list.size(); t ++)
                        {
                            ng.news_group.get(t).completeData(tcb);
                        }
                    }*/
                    //tcb.getNews(news_list, 1, e);


                }
            }
        });


    }

    static public void getSendedNews(AVUser user, NewsCallBack cb)
    {

        final AVQuery<AVObject> query = new AVQuery<>("news");
        //query.limit(len);
        //query.skip(from-1);
        query.include("news_catalog");
        query.include("news_reporter");
        query.whereEqualTo("news_reporter", user);
        final NewsCallBack tcb = cb;
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                if(tcb != null)
                {
                    if(e != null)
                    {
                        tcb.getNews(null, News.NEWS_ERROR_CATCHEXCEPTIONG, e);
                        return;
                    }
                    else if(list.size() == 0){
                        //ArrayList<News> news_list = new ArrayList<>();
                        tcb.getNews(null, News.NEWS_ERROR_NORESULT, null);
                        return;
                    }
                    else {
                        News tmp_news = null;
                        NewsGroup ng = new NewsGroup(tcb);
                        for (int t = 0; t < list.size(); t ++)
                        {
                            tmp_news = new News();
                            ng.news_group.add(tmp_news);
                            tmp_news.cur_news = list.get(t);
                            tmp_news.news_group = ng;
                            //tmp_news.completeData(tcb);
                            //news_list.add(tmp_news);
                        }
                        for (int t = 0; t < list.size(); t ++)
                        {
                            ng.news_group.get(t).completeData(tcb);
                        }
                    }
                    //tcb.getNews(news_list, 1, e);


                }
            }
        });

    }

    static public void getSearchNews(String keyword, NewsCallBack cb)
    {

        final AVQuery<AVObject> query = new AVQuery<>("news");
        //query.limit(len);
        //query.skip(from-1);
        query.include("news_catalog");
        query.include("news_reporter");
        //query.whereEqualTo("news_catalog", ca);
        query.whereContains("news_title", keyword);
        final NewsCallBack tcb = cb;
        query.findInBackground(new FindCallback<AVObject>() {
            @Override
            public void done(List<AVObject> list, AVException e) {
                if(tcb != null)
                {
                    if(e != null)
                    {
                        tcb.getNews(null, News.NEWS_ERROR_CATCHEXCEPTIONG, e);
                        return;
                    }
                    else if(list.size() == 0){
                        //ArrayList<News> news_list = new ArrayList<>();
                        tcb.getNews(null, News.NEWS_ERROR_NORESULT, null);
                        return;
                    }
                    else {
                        News tmp_news = null;
                        NewsGroup ng = new NewsGroup(tcb);
                        for (int t = 0; t < list.size(); t ++)
                        {
                            tmp_news = new News();
                            ng.news_group.add(tmp_news);
                            tmp_news.cur_news = list.get(t);
                            tmp_news.news_group = ng;
                            //tmp_news.completeData(tcb);
                            //news_list.add(tmp_news);
                        }
                        for (int t = 0; t < list.size(); t ++)
                        {
                            ng.news_group.get(t).completeData(tcb);
                        }
                    }
                    //tcb.getNews(news_list, 1, e);


                }
            }
        });

    }

/*
    static public Bundle newsToBundle(News n)
    {
        Bundle result = new Bundle();
        result.putCharSequence("news_title", n.cur_news.getString("news_title"));
        result.putCharSequence("news_title", n.cur_news.getString("news_title"));
    }
    */

    public void completeData(NewsCallBack cb)
    {
        callBack = cb;
        final News thisNews = this;

        AVFile cover = cur_news.getAVFile("news_cover");
        if(cover == null)                   //防止头像为空
        {
            Log.i("NEWSDEBUG", "null");
            news_group.succed(thisNews);
        }
        else {
            cover.getDataInBackground(new GetDataCallback() {       //下载头像
                @Override
                public void done(byte[] bytes, AVException e) {
                    if (e != null) {
                        news_group.fail(thisNews);
                    }
                    /*
                    else if (bytes == null) {
                        //Resources res =
                        //thisNews.news_cover = BitmapFactory.
                        //Toast.makeText()
                        Log.i("NEWS", "null byte");
                        news_group.succed(thisNews);

                    }
                    */
                    else {
                        thisNews.news_cover = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                        news_group.succed(thisNews);
                    }
                }
            });
        }



    }



    public void doneCompleteData(int type)
    {
        completeResult += type;
        //if(completeResult == NEWS_INFO_CATALOG + NEWS_INFO_COVER + NEWS_INFO_REPORTER)
        if(completeResult == NEWS_INFO_REPORTER + NEWS_INFO_COVER)
        {
            news_group.succed(this);
        }
    }



    /*
    public void setNews(AVObject n)
    {
        AVFile
    }
    */



}


class NewsGroup
{
    public ArrayList<News> news_group = null;
    public int gotResultItemCount = 0;
    public NewsCallBack callBack = null;

    public NewsGroup(NewsCallBack cb)
    {
        callBack = cb;
        news_group = new ArrayList<>();
    }

    public void succed(News n)
    {
        gotResultItemCount += 1;
        if(gotResultItemCount == news_group.size())
        {
            callBack.getNews(news_group, News.NEWS_ERROR_NONE, null);
        }
    }

    public void fail(News n)
    {
        news_group.remove(n);
        if(news_group.size() == 0)
        {
            callBack.getNews(null, News.NEWS_ERROR_NORESULT, null);
        }
        else if(gotResultItemCount == news_group.size())
        {
            callBack.getNews(news_group, News.NEWS_ERROR_NONE, null);
        }
    }


}
